<template>
  <el-dialog :title="sku ? '编辑SKU' : '新增SKU'" v-model="visible" width="500px" @close="close">
    <el-form :model="form" label-width="100px">
      <el-form-item label="商品名"><el-input v-model="form.item_name" /></el-form-item>
      <el-form-item label="清洗后名称"><el-input v-model="form.cleaned_name" /></el-form-item>
      <el-form-item label="品牌"><el-input v-model="form.item_brand" /></el-form-item>
      <el-form-item label="规格"><el-input v-model="form.item_specs" /></el-form-item>
      <el-form-item label="价格"><el-input v-model="form.item_price" type="number" /></el-form-item>
      <el-form-item label="分类"><el-input v-model="form.item_classification" /></el-form-item>
      <el-form-item label="基础价格"><el-input v-model="form.base_price" type="number" /></el-form-item>
      <el-form-item label="基础单位"><el-input v-model="form.base_unit" /></el-form-item>
      <el-form-item label="SKU编码"><el-input v-model="form.sku" /></el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const props = defineProps({ sku: Object })
const emit = defineEmits(['close', 'refresh'])
const visible = ref(true)
const form = ref({ ...props.sku })

watch(() => props.sku, (val) => {
  form.value = { ...val }
})

function close() {
  visible.value = false
  emit('close')
}

function submit() {
  const api = form.value.id ? '/api/sku/update' : '/api/sku/add'
  const submitData = {
    ...form.value,
    human_tag: true
  }
  axios.post(api, submitData).then(() => {
    ElMessage.success('保存成功')
    emit('refresh')
    close()
  })
}
</script>
